package com.ruoyi.project.mobile.user;

import com.ruoyi.common.utils.*;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.companyindustry.domain.CompanyIndustry;
import com.ruoyi.project.jiaqin.companyindustry.service.ICompanyIndustryService;
import com.ruoyi.project.jiaqin.followcompany.domain.JiaqinFollowCompany;
import com.ruoyi.project.jiaqin.followcompany.service.IJiaqinFollowCompanyService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.service.IJiaqinFollowMemberService;
import com.ruoyi.project.system.cardstylebackground.domain.CardStyleBackground;
import com.ruoyi.project.system.cardstylebackground.service.ICardStyleBackgroundService;
import com.ruoyi.project.system.cardstyleshow.domain.CardStyleShow;
import com.ruoyi.project.system.cardstyleshow.service.ICardStyleShowService;
import com.ruoyi.project.system.feedback.domain.Feedback;
import com.ruoyi.project.system.feedback.service.IFeedbackService;
import com.ruoyi.project.system.goods.follow.dto.GoodsFollowDto;
import com.ruoyi.project.system.goods.follow.service.IGoodsFollowService;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberExamine;
import com.ruoyi.project.system.member.enums.MemberIsVip;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.order.order.enums.OrderStatus;
import com.ruoyi.project.system.order.order.service.IOrderOrderService;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/24 16:25
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/user")
public class
ApiUserController extends BaseController {

	@Autowired
	private IMemberService memberService;
	@Autowired
	private IOrderOrderService orderOrderService;
	@Autowired
	private IGoodsGoodsService goodsGoodsService;
	@Autowired
	private IGoodsFollowService goodsFollowService;
	@Autowired
	private ServerConfig serverConfig;
	@Autowired
	private IFeedbackService feedbackService;
	@Autowired
	private IShopConfigService shopConfigService;

	/**
	 * 点击会员中心 查询用户信息
	 * @param uid
	 * @return
	 */
	@GetMapping
	public R findUser(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();
		Member oldMember = this.memberService.selectMemberById(uid);
		if(oldMember==null){
			return R.ok();
		}
		//判断用户会员是否过期
		if(oldMember.getIsVip().equals(MemberIsVip.YES.getValue())){
			if(DateUtils.getNowDate().getTime()>oldMember.getVipEndTime().getTime()){
				Member update = new Member();
				update.setIsVip(MemberIsVip.NO.getValue());
				update.setId(oldMember.getId());
				this.memberService.updateMember(update);
				oldMember.setIsVip(MemberIsVip.NO.getValue());
			}
		}
		map.put("user",oldMember);
		map.put("haoqiCoin",838);
		map.put("recommendNum",59);
		//待支付订单数量
		Integer[] toPaid = {OrderStatus.TO_BE_PAID.getValue()};
		map.put("toPayNum",this.orderOrderService.findListByStatusAndUid(toPaid,uid,0).size());
		//待发货订单数量
		Integer[] toDelivered = {OrderStatus.TO_BE_DELIVERED.getValue(),OrderStatus.CUSTOMS.getValue()};
		map.put("toDelivered",this.orderOrderService.findListByStatusAndUid(toDelivered,uid,0).size());
		//待收货订单数量
		Integer[] toReceived = {OrderStatus.TO_BE_RECEIVED.getValue()};
		map.put("toReceived",this.orderOrderService.findListByStatusAndUid(toReceived,uid,0).size());
		//待评论订单数量
		Integer[] toComment = {OrderStatus.TO_BE_COMMENT.getValue()};
		map.put("toComment",this.orderOrderService.findListByStatusAndUid(toComment,uid,0).size());
		//退款售后订单数量
//		Integer[] rxOrder = {OrderStatus.TO_BE_RETURN.getValue(),OrderStatus.TO_BE_EXCHANGE.getValue()};
		map.put("rxOrder",this.orderOrderService.findListByStatusAndUid(null,uid,1).size());
		// 名片背景
		CardStyleBackground backgroundByMemberId = cardStyleBackgroundService.findCardStyleBackgroundByMemberId(uid);
		if (backgroundByMemberId != null) {
			map.put("cardStyleBackground",backgroundByMemberId.getHeadUrl());
        } else {
			map.put("cardStyleBackground",0);
        }
		return RFactory.generateR(map);
	}

	/**
	 * 查询关注列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/follows")
	public R userFollows(@RequestParam String uid) {
		startPage();
		List<GoodsFollowDto> goodsFollowDtoList = this.goodsFollowService.findListByUid(uid);
		return RFactory.generateR(getDataTable(goodsFollowDtoList));
	}

	@Autowired
	private ICardStyleShowService cardStyleShowService;
	@Autowired
	private ICardStyleBackgroundService cardStyleBackgroundService;
	@Autowired
	private IJiaqinFollowMemberService jiaqinFollowMemberService;
	@Autowired
	private IJiaqinFollowCompanyService jiaqinFollowCompanyService;
	@Autowired
	private ICompanyService companyService;
	@Autowired
	private ICompanyIndustryService companyIndustryService;
	/**
	 * 查询用户详细信息
	 * @param memberId：用户ID
	 * @param uid：被查用户ID
	 * @return
	 */
	@GetMapping("/info")
	public R userInfo(@RequestParam String uid, String memberId){
		Member member = this.memberService.selectMemberById(uid);
		// 展示信息设置
		CardStyleShow styleShowByMemberId = cardStyleShowService.selectCardStyleShowByMemberId(uid);
		// 名片背景
		CardStyleBackground backgroundByMemberId = cardStyleBackgroundService.findCardStyleBackgroundByMemberId(uid);
		if (styleShowByMemberId == null) {
			CardStyleShow cardStyleShow = new CardStyleShow();
			cardStyleShow.setMemberId(uid);
			cardStyleShowService.insertCardStyleShow(cardStyleShow);
		}
		Company company = companyService.selectCompanyByMemberId(uid);
		if (memberId != null) {
			if (company != null) {
				member.setMemberCompanyId(company.getId());// 企业ID
				JiaqinFollowCompany jiaqinFollowCompany = jiaqinFollowCompanyService.selectJiaqinFollowMemberByMemberIdAndToCompanyId(memberId, company.getId());
				if (jiaqinFollowCompany != null) {
					member.setIsCollect(1);// 是否收藏：0-否 1-是
				} else {
					member.setIsCollect(0);
				}
			} else {
				member.setMemberCompanyId(member.getId());// 个人ID
				JiaqinFollowMember jiaqinFollowMember = jiaqinFollowMemberService.selectJiaqinFollowMemberByMemberIdAndToMemberId(memberId, uid);
				if (jiaqinFollowMember != null) {
					member.setIsCollect(1);// 是否收藏：0-否 1-是
				} else {
					member.setIsCollect(0);
				}
			}
		}
		if (member != null) {
			member.setCardStyleShow(styleShowByMemberId);
			member.setCardStyleBackground(backgroundByMemberId);
			// 行业
			if (member.getIndustry() != null) {
				CompanyIndustry industryById = companyIndustryService.selectCompanyIndustryById(member.getIndustry());
				if (industryById != null) {
					member.setIndustryName(industryById.getName());
				}
            }
			// 编辑的名字为空 名片显示微信名
			if (member.getName() == null || member.getName().equals("")) {
				member.setName(member.getUsername());
            }
		}
		return RFactory.generateR(member);
	}



	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info")
	public R updateUserInfo(@RequestBody Map map){
		String uid = (String) map.get("uid");
		String name = (String) map.get("name");
		String idCard = (String) map.get("idCard");
		String mobile = (String) map.get("mobile");
		String header = (String) map.get("headerImg");
		String back = (String) map.get("backImg");
		Member member = new Member();
		member.setId(uid);
		member.setName(name);
		member.setIdCard(idCard);
		member.setMobile(mobile);
		member.setIsExamine(MemberExamine.ADOPT.getValue());
		member.setCardHeaderImg(header);
		//上传身份证背面的图片
		member.setCardBackImg(back);
		this.memberService.updateMember(member);
		return R.ok();
	}


	/**
	 * 查询推荐人列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/recommend")
	public R userRecommend(@RequestParam String uid){
		startPage();
		Member member = new Member();
		member.setIntroUser(uid);
		List<Member> members = this.memberService.selectMemberList(member);
		return RFactory.generateR(getDataTable(members));
	}

	/**
	 * 修改用户信息
	 * @param map
	 * @return
	 */
	@PutMapping("/info2")
	public R updateUserInfo2(@RequestBody Map map){
		String username = (String) map.get("username");
		String id = (String) map.get("id");
		int sex = (int) map.get("sex");
		String headpic = (String) map.get("headpic");
		Member member = new Member();
		member.setId(id);
		member.setUsername(username);
		member.setSex(sex);
		member.setHeadpic(headpic);
		this.memberService.updateMember(member);
		return R.ok();
	}


	/**
	 * 提交意见
	 * @param feedback
	 * @return
	 */
	@PostMapping("/feedback")
	public R userFeedback(@RequestBody Feedback feedback){
		feedback.setCreated(DateUtils.getNowDate());
		this.feedbackService.insertFeedback(feedback);
		return R.ok();
	}

	@GetMapping("/share")
	public R share(@RequestParam String uid){
		HashMap<String, Object> map = new HashMap<>();
		Member member = this.memberService.selectMemberById(uid);
		if(StringUtils.isBlank(member.getQrCode())){
			String qrCode = WeixinUtils.getQrCodeImg(member);
			member.setQrCode(qrCode);
			Member update = new Member();
			update.setQrCode(qrCode);
			update.setId(uid);
			this.memberService.updateMember(update);
		}
		map.put("avator",member.getHeadpic());
		map.put("name",member.getUsername());
		map.put("InvitationCode","");
		map.put("shopId","");
		map.put("imgurl",java.util.Arrays.asList(this.shopConfigService.findAll().get(0).getShareImg()));
		map.put("qrCode",member.getQrCode());
		return RFactory.generateR(map);
	}

}
